<?php
	function f202($sql){
		//create time handle instance

		//jsspecialchars("/");

		$dataHandle = new DataHandle();
		$dataHandle->setRequest("202");


		$action = getValue($_GET['action']);

		if($action == "getItems"){
			$format = array(
				"startTime" 	=> "dateh i A  m d Y",
				"endTime" 		=> "dateh i A",
				"modifiedDate"	=> "datem/d/Y g:i:s A",
			);
			$dataHandle->setFormat($format);
			$query = getItemJSONQuery();
			print($dataHandle->printJSON($sql, $query));
		}
		elseif($action == "addNewItem"){
			addNewItem($sql);
		}
		elseif($action == "saveItem"){
			saveItem($sql);
		}
		elseif($action == "deleteItem"){
			deleteItem($sql);
		}
		else{
			$temp = array();
			$temp['error'] = "202: Bad request";
			print(json_encode($temp));
		}

		unset($dataHandle);
	}

	function getItemJSONQuery(){

		$ID = getValue($_GET['id']);

		$queryString = 
			"
			SELECT
			lin_produced.order_id as productionOrder,
			lin_produced.shift,
			CAST(
				ROUND(
						(100 * dbo.lin_Produced.pouches)   
						/(dbo.lin_Produced.speed
						* dbo.lin_calc_working_time(
											dbo.lin_Produced.start_time, 
											dbo.lin_Produced.end_time, 
											dbo.lin_Products.line_id
											)
					), 2) 
              	AS numeric(8, 1)) AS PE,
			lin_produced.minutes,
			lin_products.description,
			lin_produced.product_id  as recipeNo,
			lin_produced.start_time as startTime, 
			lin_produced.end_time as endTime, 
			lin_products.speed as speed,
			lin_produced.speed as actualSpeed,
			lin_produced.line_id as line,
			lin_produced.notes as notes,
			lin_produced.modified_by as modifiedBy, 
			lin_produced.modified_date as modifiedDate,
			lin_produced.l1_user_name as userName,
			lin_produced.attendant as attendant,
			lin_produced.packers as packers,
			lin_produced.roll_code_bin_change as rollCodeBin,
			lin_produced.filling_machine as fillingMachine,
			lin_produced.no_mix_no_materials as noMixNoMaterial,
			lin_produced.cartoner_wrapper as cartonerWrapper,
			lin_produced.unaccounted as unaccounted,
			lin_produced.check_weighter as weighter,
			lin_produced.change_over as changeOver,
			lin_produced.casepacker as casePacker,
			lin_produced.pouches as pouches,
			lin_produced.pouches_total as pouchesTotal,
			lin_produced.units as cases,
			lin_produced.units_total as casesTotal,
			lin_produced.web as web,
			lin_produced.web_total as webTotal,
			lin_produced.powder as powder,
			lin_produced.powder_total as powderTotal,
			dbo.alm_get_alarm_sum_for_prod( start_time, end_time, id_pk) as alm_sum

			FROM lin_produced
			INNER JOIN lin_products ON lin_produced.product_id=lin_products.product_id
			WHERE lin_produced.id_pk = ".$ID.";";


		return $queryString;
	}
	function addNewItem($sql){

		$temp = array();
		$temp['error'] = "202: Not Implemented";
		print json_encode($temp);
	}

	function saveItem($sql){

		$temp = array();
		$temp['error'] = "202: Not Implemented";
		print json_encode($temp);
	}
	function deleteItem($sql){

		$temp = array();
		$temp['error'] = "202: Not Implemented";
		print json_encode($temp);
	}
?>